﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using Crude.Core;
using Crude.Core.Repository;
using Crude.Common;

namespace Crude.Tasks
{
    public class PublishLocal : Task
    {
        [Required]
        public string ProjectDirectory { get; set; }

        [Required]
        public string ProjectName { get; set; }

        [Required]
        public string Configuration { get; set; }

        [Required]
        public string OutputDirectory {get;set;}
        

        public override bool Execute()
        {
            Log.LogMessage("Publish local started...");
            LocalRepository localRepository = new LocalRepositoryBuilder().Build();

            string pomFileLocation = string.Format("{0}\\{1}.pom.xml", ProjectDirectory, ProjectName);
            //string sourceDirectory =string.Format("{0}\\{1}", ProjectDirectory, OutputDirectory); 

            ProjectObjectModel pom = new ProjectObjectModelLoader(pomFileLocation).Load();

            try
            {
                localRepository.UploadProject(pomFileLocation, OutputDirectory, pom, Configuration);
            }
            catch (Exception ex)
            {
                Log.LogError(ex.Message);
                return false;
            }

            Log.LogMessage("Publish local succeeded");
           
            return true;
        }
    }
}
